﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;


/// <summary>
/// 通用的倒计时，以及不同的游戏需要不同的自我属性
/// </summary>
public class CountDown : MonoBehaviour
{
    //游戏结束
    public static bool gameEnd;
    private int CountDownNum ;//等做好可以直接闹到需要的时间
    //5秒倒计时
    private int count5 = 5;
    //倒计时的图片
    private Image countDown5;
    //是否有点击返回按钮
    public static bool isreturn;
    //倒计时的显示
    private Text countDown;
    //返回按钮
    private Button btnReturn;
    //结束面板
    private GameObject gameOverPlane;

    #region 全民捞鱼
    //抓鱼的抓把
    private GameObject dragFish;
    //QuanMinLaoYu
    private GameObject fishSpawn;
    #endregion

    #region 高空跳伞
    GameObject player;
    GameObject produceObj;
    Image countDownImg;
    GameObject backGround;
    bool countDown5_tiaosan = false;
    #endregion

    void Awake()
    {
        backGround = GameObject.Find("BackGround");
        btnReturn = GameObject.Find("BtnReturn").GetComponent<Button>();
        btnReturn.interactable = false;
        switch (OSBridgeManager.Instance.GameName)
        {
            case "QuanMinLaoYu":
                CountDownNum = 60;
                dragFish = GameObject.Find("DragFish");
                dragFish.SetActive(false);
                fishSpawn = GameObject.Find("FishSpawn");
                fishSpawn.SetActive(false);
                break;
            case "GaoKongTiaoSan":
                CountDownNum = 100;
                countDownImg = GameObject.Find("CountDown/CountDownImg").GetComponent<Image>();
                player = GameObject.Find("MobileSingleStickControl/Player");
                player.GetComponentsInChildren<Image>()[0].enabled = false;
                player.GetComponentsInChildren<Image>()[1].enabled = false;
                player.GetComponentsInChildren<Image>()[2].enabled = false;
                produceObj = GameObject.Find("ProduceObj");
                produceObj.SetActive(false);
                break;
        }

        isreturn = false;
        gameOverPlane = GameObject.Find("GameOverPlane");

        countDown5 = GameObject.Find("CountDown5").GetComponent<Image>();
        countDown5.gameObject.SetActive(false);
        countDown = GameObject.Find("CountDown").GetComponent<Text>();
    }

    private void OnInitGameHandler(string gameName)
    {
        
    }

    IEnumerator Start()
    {
        GameObject.Find("ScoreUI/Score").GetComponent<Text>().text = ScoreScript.QuanMinLaoYuScore.ToString();
        yield return new WaitForSeconds(1.0f);
        StartCoroutine("CountDown5");
    }

    IEnumerator CountDown5()
    {
        ScoreScript.BeZero();
        gameEnd = false;
        countDown5 .gameObject.SetActive(true);
        while (true)
        {
            count5--;
            yield return new WaitForSeconds(1);
            //更改图片
            countDown5.overrideSprite = Resources.Load<Sprite>(OSBridgeManager.Instance.GameName + "/UI/" + OSBridgeManager.Instance.GameName + "_" + count5);
            if (count5 == 0)
            {
                countDown5.GetComponent<RectTransform>().sizeDelta = new Vector2(274, 274);
            }
            if (count5 == -1)
            {
                btnReturn.interactable = true;
                Destroy(countDown5.gameObject);
                //各个游戏不同的一些逻辑
                switch (OSBridgeManager.Instance.GameName)
                {
                    case "QuanMinLaoYu":
                        dragFish.gameObject.SetActive(true);
                        fishSpawn.SetActive(true);
                        fishSpawn.GetComponent<FishSpawn>().fish_count = 0;
                        StartCoroutine("AppointCountDown");
                        break;
                    case "GaoKongTiaoSan":
                        countDown5_tiaosan = true;
                        player.GetComponentsInChildren<Image>()[0].enabled = true;
                        player.GetComponentsInChildren<Image>()[1].enabled = true;
                        player.GetComponentsInChildren<Image>()[2].enabled = true;
                        produceObj.SetActive(true);
                        StartCoroutine("AppointCountDown");
                        break;
                }
                break;
            }
        }
    }
    //自定义的倒计时
    IEnumerator AppointCountDown()
    {

        while (isreturn == false)
        {
            CountDownNum--;
            yield return new WaitForSeconds(1);
            if (CountDownNum <= 0)
            {
                gameEnd = true;
                //各游戏具体细节
                switch (OSBridgeManager.Instance.GameName)
                {
                    case "QuanMinLaoYu":

                        dragFish.gameObject.SetActive(true);
                        GameObject.Find("FishSpawn").GetComponent<FishSpawn>().fish_count = 0;
                        Destroy(GameObject.Find("FishSpawn"));
                        break;
                    case "GaoKongTiaoSan":
                        countDown5_tiaosan = false;
                        Destroy(produceObj);
                        player.GetComponentsInChildren<Image>()[0].enabled = false;
                        player.GetComponentsInChildren<Image>()[1].enabled = false;
                        InvokeRepeating("SanScale", 1, 0.02f);
                        break;
                }
                if (OSBridgeManager.Instance.GameName == "GaoKongTiaoSan")
                {
                    yield return new WaitForSeconds(2f);
                }
                gameOverPlane.GetComponent<RectTransform>().localScale = Vector3.Lerp(Vector3.zero, Vector3.one, Time.deltaTime * 100f);
                btnReturn.interactable = false;
                break;
            }
            else
            {
                switch (OSBridgeManager.Instance.GameName)
                {
                    case "QuanMinLaoYu":
                        countDown.text = CountDownNum.ToString();
                        break;
                    case "GaoKongTiaoSan":
                        backGround.GetComponent<Image>().color = new Color(backGround.GetComponent<Image>().color.r, backGround.GetComponent<Image>().color.g, backGround.GetComponent<Image>().color.b, backGround.GetComponent<Image>().color.a + 0.004f);
                        //Debug.Log(backGround.GetComponent<Image>().color.a);
                        countDownImg.fillAmount -= 0.01f;
                        break;
                }
            }
        }
    }
    void Update()
    {
        if (countDown5_tiaosan)
        {
            backGround.transform.localScale = new Vector3(backGround.transform.localScale.x + 0.0002f, backGround.transform.localScale.y + 0.0002f, backGround.transform.localScale.z + 0.0002f);
        }
    }
    //高空跳伞的伞变化
    void SanScale()
    {
        if (GameObject.Find("San").transform.localScale == Vector3.one)
        {
            CancelInvoke("SanScale");
        }
        GameObject.Find("San").transform.localScale = new Vector3(GameObject.Find("San").transform.localScale.x + 0.1f, GameObject.Find("San").transform.localScale.y + 0.1f, GameObject.Find("San").transform.localScale.z + 0.1f);
    }
    //给返回面板的取消按钮调用的协程
    //public void ContinueAppointCountDown()
    //{
    //    StartCoroutine("AppointCountDown");
    //}
}
